﻿//JumbotCms_4.1 Final
using System;
using System.Web;
using System.Data;
using Jumbot.Common;
using JumbotCms.Common;
namespace JumbotCms.WebFile.MTag
{
    public partial class _view_membermanage_ajax : JumbotCms.API.UserCenter
    {
        private string _operType = string.Empty;
        private string _response = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!CheckFormUrl())
            //{
            //    Response.Write("提交有误");
            //    Response.End();
            //}

            this._operType = q("oper");
            switch (this._operType)
            {
                case "ajaxGetList":
                    ajaxGetList();
                    break;
                case "ajaxManage":
                    ajaxManage();
                    break;
                default:
                    DefaultResponse();
                    break;
            }
            Response.Write(this._response);
        }
        private void DefaultResponse()
        {
            User_Load("", "json");
        }
        private void ajaxGetList()
        {
            User_Load("", "json");
            int page = Int_ThisPage();
            int PSize = Str2Int(q("pagesize"), 10);
            int countNum = 0;
            string sqlStr = "";
            string joinStr = "a.UserId=B.Id";
            string whereStr1 = "A.mtagid=" + Str2Str(q("mtagid"));
            string whereStr2 = "mtagid=" + Str2Str(q("mtagid"));
            string sort = Str2Str(q("sort"));
            if (sort != "0")
            {
                switch (sort)
                {
                    case "1"://Power=8,9
                        whereStr1 += " AND A.Power>=8";
                        whereStr2 += " AND Power>=8";
                        break;
                    case "2"://Power=0
                        whereStr1 += " AND A.Power=-0";
                        whereStr2 += " AND Power=0";
                        break;
                    case "3"://Power=-1
                        whereStr1 += " AND A.Power=-1";
                        whereStr2 += " AND Power=-1";
                        break;
                    case "4"://Power=-2
                        whereStr1 += " AND A.Power=-2";
                        whereStr2 += " AND Power=-2";
                        break;
                }
            }
            doh.Reset();
            doh.ConditionExpress = whereStr2;
            countNum = doh.Count("jcms_normal_mtag_member");
            sqlStr = Jumbot.Common.staticFunction.GetSql("a.id as memberid,a.UserId as memberuserid,a.Power as memberPower,a.MTagId as memberMTagId,b.UserName as memberusername,(select count(id) FROM [jcms_normal_mtag] WHERE UserId=a.UserId AND Id=" + Str2Str(q("mtagid")) + ") as memberisfounder", "jcms_normal_mtag_member", "jcms_normal_user", "Id", PSize, page, "asc", joinStr, whereStr1, whereStr2);
            doh.Reset();
            doh.SqlCmd = sqlStr;
            DataTable dt = doh.GetDataTable();
            this._response = "{result :\"1\"," +
                "returnval :\"操作成功\"," +
                "pagerbar :\"" + Jumbot.Common.HtmlPager.GetPageBar(3, "js", 2, countNum, PSize, page, "javascript:ajaxList(<#page#>);") + "\"," +
                Jumbot.Common.JsonHelp.DtToSON(dt, (PSize * (page - 1))) +
                "}";
            dt.Clear();
            dt.Dispose();
        }
        /// <summary>
        /// 改变成员权限
        /// </summary>
        private void ajaxManage()
        {
            string ids = f("ids");
            string[] idValue;
            idValue = ids.Split(',');
            for (int i = 0; i < idValue.Length; i++)
            {
                if (q("toPower") != "-9")
                {
                    doh.Reset();
                    doh.ConditionExpress = "mtagid=@mtagid and userid=@userid";
                    doh.AddConditionParameter("@mtagid", Str2Str(q("mtagid")));
                    doh.AddConditionParameter("@userid", idValue[i]);
                    doh.AddFieldItem("Power", q("toPower"));
                    doh.Update("jcms_normal_mtag_member");
                }
                else
                {
                    doh.Reset();
                    doh.ConditionExpress = "mtagid=@mtagid and userid=@userid";
                    doh.AddConditionParameter("@mtagid", Str2Str(q("mtagid")));
                    doh.AddConditionParameter("@userid", idValue[i]);
                    doh.Delete("jcms_normal_mtag_member");
                    //统计减一
                    doh.Reset();
                    doh.ConditionExpress = "id=@id";
                    doh.AddConditionParameter("@id", Str2Str(q("mtagid")));
                    doh.Substract("jcms_normal_mtag", "MemberTotal", 1);
                }
            }
            this._response = JsonResult(1, "操作成功");
        }
    }
}
